<?php

namespace App\Transformers\Order;

use App\Models\Order;
use App\Transformers\User\UserTransformer;
use League\Fractal\TransformerAbstract;

class OrderShowTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['orderEconomize','user','promoter'];

    public function transform(Order $data)
    {
        return [
            'id' => $data->id,
            'user_id' => $data->user_id ?? '',
            'promoter_id' => $data->promoter_id ?? '',
            'user_name' => $data->user_name ?? '',
            'user_phone' => $data->user_phone ?? '',
            'user_address' => $data->user_address ?? '',
            'order_number' => $data->order_number ?? '',
            'total_num' => $data->total_num ?? '',
            'total_price' => $data->total_price ?? '',
            'pay_postage' => $data->pay_postage ?? '',
            'pay_price' => $data->pay_price ?? '',
            'pay_time' => $data->pay_time ?? '',
            'pay_type' => $data->pay_type ?? '',
            'pay_type_text' => $data->orderPayType[$data->pay_type] ?? '',
            'status' => $data->status ?? '',
            'status_text' => $data->orderStatusTypeEnum[$data->status] ?? '',
            'use_integral' => $data->use_integral ?? '',
            'coupon_price' => $data->coupon_price ?? '',
            'refund_status' => $data->refund_status ?? '',
            'refund_price' => $data->refund_price ?? '',
            'refund_status_text' => $data->refundStatusTypeEnum[$data->refund_status] ?? '',
            'refund_reason_wap_explain' => $data->refund_reason_wap_explain ?? '',
            'mark' => $data->mark ?? '',
            'delivery_name' => $data->delivery_name ?? '',
            'delivery_type' => $data->delivery_type ?? '',
            'delivery_id' => $data->delivery_id ?? '',
            'verify_code' => $data->verify_code ?? '',
            'promoter' => $data->promoter ?? '',
            'user' => $data->user ?? '',
            'orderEconomize' => $data->orderEconomize ?? '',
            'created_at' => $data->created_at ? $data->created_at->format("Y-m-d H:i:s") : '',
            'updated_at' => $data->updated_at ? $data->updated_at->format("Y-m-d H:i:s") : '',
        ];
    }

    /**
     * 订单用户信息
     */
    public function includeUser(Order $order)
    {
        return $this->item($order->user,new UserTransformer());
    }

    /**
     * 订单使用优惠
     */
    public function includeOrderEconomize(Order $order)
    {
        return $this->item($order->orderEconomize, new OrderEconomizesTransformer());
    }

    /**
     * 订单推广人信息
     */
    public function includePromoter(Order $order)
    {
        return $this->item($order->promoter, new UserTransformer());
    }
}
